package com.qx.chat.system.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qx.chat.system.entity.Group;
import com.qx.chat.system.service.GroupService;
import com.qx.chat.system.service.OrgService;

/**
 * 
 * 群组管理
 * 
 * @ClassName: GroupController
 * @version V1.0
 * @date 2015年12月25日
 * @author cyf
 */
@Controller
@Scope(value = "prototype")
@RequestMapping(value = "/group")
public class GroupController {
	@Autowired
	private GroupService groupServiceImpl;
	@Autowired
	private OrgService orgServiceImpl;

	@RequestMapping("/save")
	public void save(Group group, String ownerLoginName, HttpServletRequest request, HttpServletResponse response) {
		try {
			group = groupServiceImpl.createOrUpdate(group, ownerLoginName);
			String jsonStr = JSONObject.toJSONString(group);
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			PrintWriter out = response.getWriter(); // 获取写入对象
			out.print(jsonStr); // 将json数据写入流中
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("/view")
	public void view(String groupId, HttpServletRequest request, HttpServletResponse response) {
		try {
			String jsonStr = groupServiceImpl.view(groupId);
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			PrintWriter out = response.getWriter(); // 获取写入对象
			out.print(jsonStr); // 将json数据写入流中
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("/dissolve")
	public void dissolve(String groupId, HttpServletRequest request, HttpServletResponse response) {
		try {
			String jsonStr = groupServiceImpl.dissolve(groupId);
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			PrintWriter out = response.getWriter(); // 获取写入对象
			out.print(jsonStr); // 将json数据写入流中
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("/addMembers")
	public void addMembers(String groupId,String memberIds, HttpServletRequest request, HttpServletResponse response) {
		try {
			String jsonStr = groupServiceImpl.addMembers(groupId,memberIds);
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			PrintWriter out = response.getWriter(); // 获取写入对象
			out.print(jsonStr); // 将json数据写入流中
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@RequestMapping("/getOrgTree")
	public void getOrgTree(HttpServletRequest request, HttpServletResponse response) {
		try {
			JSONArray jsonStr = orgServiceImpl.findOrgTreeJsonData();
			response.setCharacterEncoding("UTF-8"); // 设置编码格式
			response.setContentType("text/html"); // 设置数据格式
			PrintWriter out = response.getWriter(); // 获取写入对象
			out.print(jsonStr.toJSONString()); // 将json数据写入流中
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}